package com.legal.aid.app.controller;


import com.legal.aid.app.domain.dto.OrderAddDto;
import com.legal.aid.app.domain.dto.OrderUpdateDto;
import com.legal.aid.app.domain.vo.OrderVo;
import com.legal.aid.app.service.IAppOrderService;
import com.legal.aid.common.annotation.Log;
import com.legal.aid.common.constant.OrderConstants;
import com.legal.aid.common.core.controller.BaseController;
import com.legal.aid.common.core.domain.AjaxResult;
import com.legal.aid.common.core.page.TableDataInfo;
import com.legal.aid.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/order")
public class AppOrderController extends BaseController {
    @Autowired
    private IAppOrderService appOrderService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OrderVo orderVo)
    {
        startPage();
        List<OrderVo> list = appOrderService.selectOrderList(orderVo);
        return getDataTable(list);
    }
    /**
     * 支付订单
     */
    @PutMapping(value = "/pay/{orderNO}")
    public AjaxResult PayOrder(@PathVariable("orderNO") String orderNo)
    {
        String msg = appOrderService.payOrder(orderNo);
        return  msg==OrderConstants.PAY_SUCCESS?success(msg):AjaxResult.error(501,msg);
    }

    /**
     * 取消订单
     */
    @Log(title = "订单取消", businessType = BusinessType.INSERT)
    @PutMapping(value = "/cancel/{orderNO}")
    public AjaxResult CancelOrder(@PathVariable("orderNO") String  orderNO)
    {
        return success(appOrderService.cancelOrder(orderNO));
    }

    /**
     * 支付成功
     */
//    @PutMapping(value = "/pay/{orderNO}")
//    public AjaxResult PayOrderSuccess(@PathVariable("orderNO") String orderNo)
//    {
//        return success(appOrderService.selectOrderById(orderNo));
//    }



    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{orderNO}")
    public AjaxResult getInfo(@PathVariable("orderNO") String orderNo)
    {
        return success(appOrderService.selectOrderById(orderNo));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderAddDto orderDto)
    {
        String result = appOrderService.insertOrder(orderDto);
        if(result.equals("0")){
            return error("订单创建失败");
        }
        return AjaxResult.success(result);
    }

    /**
     * 修改订单
     */
    @Log(title = "订单修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderUpdateDto orderUpdateDto)
    {
        return success(appOrderService.updateOrder(orderUpdateDto));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return success(appOrderService.deleteOrderByIds(ids));
    }
}
